﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpriteMaker
{
    public partial class FrameWorkingPanel : Control
    {
        //private Image texture;
        private int xCoord, yCoord;
        private float zoom;
        private int lastMouseX, lastMouseY;
        private Frame activeFrame;
        private FrameModule activeFrameModule;
        public formMain formInstance;
        private int delta = 2;
        private enum MouseModuleState
        {
            None,
            Drag,
        }
        private MouseModuleState mouseState = MouseModuleState.None;
        public FrameWorkingPanel()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            xCoord = this.ClientSize.Width / 2;
            yCoord = this.ClientSize.Height / 2;
        }
        public Frame GetActiveFrame()
        {
            return activeFrame;
        }
        public FrameModule GetActiveFrameModule()
        {
            return activeFrameModule;
        }
        /*
        public void SetImage(Image im)
        {
            texture = im;
            this.Refresh();
        }
        */
        public void SetActiveFrame(Frame frame)
        {
            activeFrame = frame;
            this.Refresh();
        }
        public void SetActiveFrameModule(FrameModule fm)
        {
            activeFrameModule = fm;
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            pe.Graphics.Clear(Color.Gray);
            if (activeFrame != null && formInstance.GetImage() != null)
            {
                foreach (FrameModule frameModule in activeFrame.frameModuleList)
                {
                    pe.Graphics.DrawImage(formInstance.GetImage()
                    , new Rectangle(frameModule.x + xCoord, -frameModule.y + yCoord - frameModule.module.height, frameModule.module.width, frameModule.module.height)
                        , new Rectangle(frameModule.module.x, -frameModule.module.y, frameModule.module.width, frameModule.module.height), GraphicsUnit.Pixel);
                }
            }
            Pen line = new Pen(Color.DarkRed);
            line.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            pe.Graphics.DrawLine(line, new Point(0, Height/2), new Point(Width, Height/2));
            pe.Graphics.DrawLine(line, new Point(Width/2, 0), new Point(Width/2, Height));
        }

        private void FrameWorkingPanel_Resize(object sender, EventArgs e)
        {
            xCoord = this.ClientSize.Width / 2;
            yCoord = this.ClientSize.Height / 2;
            this.Refresh();
        }

        private void FrameWorkingPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (activeFrameModule != null && Util.PointInRect(e.X, e.Y, activeFrameModule.x + xCoord, -activeFrameModule.y + yCoord - activeFrameModule.module.height
                    , activeFrameModule.module.width, activeFrameModule.module.height))
                {
                    mouseState = MouseModuleState.Drag;
                }
            }
            lastMouseX = e.X;
            lastMouseY = e.Y;
        }

        private void FrameWorkingPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (mouseState == MouseModuleState.Drag)
                {
                    activeFrameModule.x += e.X - lastMouseX;
                    activeFrameModule.y -= e.Y - lastMouseY;
                    formInstance.UpdateActiveFrameModule();
                    this.Refresh();
                }
            }
            lastMouseX = e.X;
            lastMouseY = e.Y;
        }

        private void FrameWorkingPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (mouseState == MouseModuleState.Drag)
                {
                    activeFrameModule.x += e.X - lastMouseX;
                    activeFrameModule.y -= e.Y - lastMouseY;
                    formInstance.UpdateActiveFrameModule();
                    this.Refresh();
                }
                mouseState = MouseModuleState.None;
            }
        }
    }
}
